package com.farm.wcp.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.authority.service.OutuserServiceInter;
import com.farm.authority.service.UserServiceInter;
import com.farm.cbtg.domain.ex.CatalogDomain;
import com.farm.core.page.ViewMode;
import com.farm.sfile.WdapFileServiceInter;
import com.farm.template.domain.Template;
import com.farm.template.domain.TemplateDir;
import com.farm.template.domain.TemplateFile;
import com.farm.template.service.TemplateServiceInter;
import com.farm.template.service.TemplatedirServiceInter;
import com.farm.template.service.TemplatefileServiceInter;
import com.farm.wcp.util.ThemesUtil;
import com.farm.web.WebUtils;

@RequestMapping("/temveiw")
@Controller
public class TemplateWebController extends WebUtils {
	private final static Logger log = Logger.getLogger(TemplateWebController.class);
	@Resource
	private OutuserServiceInter outUserServiceImpl;
	@Resource
	private UserServiceInter userServiceImpl;
	@Resource
	private WdapFileServiceInter wdapFileServiceImpl;
	@Resource
	private TemplateServiceInter templateServiceImpl;
	@Resource
	private TemplatedirServiceInter templateDirServiceImpl;
	@Resource
	private TemplatefileServiceInter templateFileServiceImpl;

	/**
	 * 模板管理界面首页
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView pubLogin(String templateid, HttpSession session, HttpServletRequest request) {
		ViewMode mode = ViewMode.getInstance();
		try {
			CatalogDomain domian = CatalogDomain.TEMPLATE;
			Template template = templateServiceImpl.getTemplateEntity(templateid);
			return mode.putAttr("template", template).putAttr("domian", domian)
					.returnModelAndView(ThemesUtil.getThemePath() + "/template/index");
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e)
					.returnModelAndView(ThemesUtil.getThemePath() + "/simple-500");
		}
	}

	/**
	 * 加载文件信息
	 * 
	 * @param templateid
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/loadFileInfo")
	public ModelAndView loadFileInfo(String templateFileId, HttpSession session, HttpServletRequest request) {
		ViewMode mode = ViewMode.getInstance();
		try {
			TemplateFile tfile = templateFileServiceImpl.getTemplatefileEntity(templateFileId);
			Template template = templateServiceImpl.getTemplateEntity(tfile.getTemplateid());
			return mode.putAttr("tfile", tfile).putAttr("template", template)
					.returnModelAndView(ThemesUtil.getThemePath() + "/template/commons/fileInfo");
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e)
					.returnModelAndView(ThemesUtil.getThemePath() + "/simple-500");
		}
	}

	/**
	 * 加载文件内容
	 * 
	 * @param templateid
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/loadFileText")
	@ResponseBody
	public Map<String, Object> loadFileText(String templateFileId, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			TemplateFile tfile = templateFileServiceImpl.getTemplatefileEntity(templateFileId);
			String exname = wdapFileServiceImpl.getExName(tfile.getFilename());
			if (StringUtils.isBlank(exname)) {
				exname = "text";
			}
			return page.putAttr("tfile", tfile).putAttr("exname", exname).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 加载文件内容
	 * 
	 * @param templateid
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveFileText")
	@ResponseBody
	public Map<String, Object> saveFileText(String templateFileId, String text, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			if (StringUtils.isNotBlank(templateFileId)) {
				templateFileServiceImpl.saveText(templateFileId, text, getCurrentUser(session));
			}
			return page.returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 文件重命名
	 * 
	 * @param templateid
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveFileName")
	@ResponseBody
	public Map<String, Object> saveFileName(String templateFileId, String name, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			templateFileServiceImpl.saveName(templateFileId, name, getCurrentUser(session));
			return page.returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 修改目录
	 * 
	 * @param templateid
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/editDir")
	@ResponseBody
	public Map<String, Object> editDir(String templateDirId, String name, Integer sort, String note,
			HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			TemplateDir entity = templateDirServiceImpl.getTemplatedirEntity(templateDirId);
			entity.setName(name);
			entity.setSort(sort);
			entity.setPcontent(note);
			templateDirServiceImpl.editTemplatedirEntity(entity, getCurrentUser(session));
			return page.returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 移動文件
	 * 
	 * @param templateid
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/moveFileToDir")
	@ResponseBody
	public Map<String, Object> moveFileToDir(String templateFileId, String templateDirId, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			templateFileServiceImpl.bindDir(templateFileId, templateDirId, getCurrentUser(session));
			return page.returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 创建目录
	 * 
	 * @return
	 */
	@RequestMapping("/creatdir")
	@ResponseBody
	public Map<String, Object> creatdir(String templateid, String parentid, String name, Integer sort, String note,
			HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			TemplateDir dir = new TemplateDir();
			dir.setName(name);
			dir.setSort(sort);
			dir.setPcontent(note);
			dir.setParentid(parentid);
			dir.setTemplateid(templateid);
			dir = templateDirServiceImpl.insertTemplatedirEntity(dir, getCurrentUser(session));
			return page.putAttr("node", dir).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 创建文件
	 * 
	 * @return
	 */
	@RequestMapping("/creatfile")
	@ResponseBody
	public Map<String, Object> creatfile(String templateid, String dirid, String name, String note,
			HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			TemplateFile tfile = new TemplateFile();
			tfile.setFilename(name);
			tfile.setPcontent(note);
			tfile.setDirid(dirid);
			tfile.setTemplateid(templateid);
			tfile = templateFileServiceImpl.insertTemplatefileEntity(tfile, getCurrentUser(session));
			return page.putAttr("node", tfile).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 刪除文件
	 * 
	 * @return
	 */
	@RequestMapping("/delfile")
	@ResponseBody
	public Map<String, Object> delfile(String templateid, String id, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			templateFileServiceImpl.deleteTemplatefileEntity(id, getCurrentUser(session));
			return page.returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 刪除目錄
	 * 
	 * @return
	 */
	@RequestMapping("/deldir")
	@ResponseBody
	public Map<String, Object> deldir(String templateid, String id, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			templateDirServiceImpl.deleteTemplatedirEntity(id, getCurrentUser(session));
			return page.returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

	/**
	 * 加载文件文件夹
	 * 
	 * @return
	 */
	@RequestMapping("/loadfiles")
	@ResponseBody
	public Map<String, Object> loadfiles(String templateid, HttpSession session) {
		try {
			ViewMode page = ViewMode.getInstance();
			List<TemplateDir> dirs = templateDirServiceImpl.getTemplateDirs(templateid);
			List<TemplateFile> files = templateFileServiceImpl.getTemplateFiles(templateid);
			return page.putAttr("dirs", dirs).putAttr("files", files).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}
}
